package mobi.weq.servlets.user;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mobi.weq.dao.UserDao;
import mobi.weq.model.User;
import mobi.weq.util.Util;

/**
 * Servlet implementation class RegisterUser
 */
@WebServlet("/user/delete")
public class DeleteUser extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user = Util.getUserFromSession(request, response);
		if (user != null) {
			// String password = request.getParameter("password");
			// if (password.equals(user.getPassword())) {
			UserDao ud = new UserDao();
			try {
				ud.deleteUser(user);
				HttpSession session = request.getSession(true);
				session.invalidate();
				response.sendRedirect("../login");
			} catch (SQLException e) {
				System.out.println(e);
			}
			// }
		} else {
			response.sendRedirect("../login");
		}
	}
}
